package br.com.gsa.bean;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import br.com.gsa.model.Task;
import br.com.gsa.service.TaskService;


@Controller("taskHome")
@Scope("request")
public class TaskHome extends BaseView {

	private static final Logger logger = LoggerFactory.getLogger(TaskHome.class);
	
	
	private Task task = new Task();
	
	private List<Task> tasks;

	private Task taskUpdate;
	
	private boolean edicao;
	
	private String idTask;
	
	@Autowired
	private TaskService taskService;

	public String getMessage() {
		logger.debug("Returning message from task home bean");
		return "Hello from Spring";
	}	

	public String carregarTask(){
		return "task";
	}
	
	
	public void deleteTask(){
	       
		taskService.deleteById(Long.valueOf(idTask));
		//taskService.delete(task);
		atualizaDataTask();
	}

	private void atualizaDataTask() {
		tasks = taskService.recuperaTasks();
		
	}

	public void saveTask() {
	   if(task.getId() == null){
			  taskService.save(task);
			  this.task = new Task();
	   }
	   else{
			   update();
			   this.task = new Task();
	   }
	   
	}

	public List<Task> getTasks() {
		if (tasks == null) {
			tasks = taskService.recuperaTasks();
		}
		return tasks;
	}

	
	public void update(){
		taskService.update(task);
	}
	
	public Task getTask() {
			return task ;
	}


	public Task getTaskUpdate() {
		return taskUpdate;
	}

	public void setTaskUpdate(Task taskUpdate) {
		this.taskUpdate = taskUpdate;
	}

	public void setTask(Task task) {
		this.task = task;
	}


	public boolean isEdicao() {
		return edicao;
	}


	public void setEdicao(boolean edicao) {
		this.edicao = edicao;
	}


	public String getIdTask() {
		return idTask;
	}


	public void setIdTask(String idTask) {
		this.idTask = idTask;
	}
	
	
}
